package com.ywm.eduService.controller;


import com.ywm.commonUtil.R;
import com.ywm.eduService.client.VodClient;
import com.ywm.eduService.entity.EduVideo;
import com.ywm.eduService.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/eduService/edu-video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;
    /**
     * 添加小节
     * @return
     */
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    /**
     * 删除小节
     */
    // TODO 后面需要完善，删除小节的时候同时删除里面的视频
    @DeleteMapping("/deleteVideoById/{id}")
    public R deleteVideo(@PathVariable String id){
        //先删视频，在删小节
        EduVideo video = videoService.getById(id);
        //视频id
        String videoSourceId = video.getVideoSourceId();
        if (videoSourceId != null) {
            //根据视频id，远程调用删除
            vodClient.removeAlyVideo(videoSourceId);
        }
        boolean b = videoService.removeById(id);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("根据id查询小节")
    @GetMapping("/getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable("videoId") String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }

    //修改小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }
}

